/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scm.struts.actions;

import com.scm.hibernate.entities.Student;
import com.scm.struts.forms.EditStudentForm;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author RANGER
 */
public class UpdateStudentAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static String path = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        EditStudentForm esf = (EditStudentForm) form;
        // ArrayList<Student> li=(ArrayList<Student>)request.getSession().getAttribute("detail");

        System.out.println("Lname ="+esf.getLname());
        Student s = new Student();
        s.setId(esf.getId());
        s.setFname(esf.getFname());
        s.setLname(esf.getLname());
        s.setAddress(esf.getAddress());
        s.setContact(esf.getContact());
        s.setEmail(esf.getEmail());
        s.setPassword("");
        s.setDateofbirth(esf.getDateofbirth());
        s.setCategory(esf.getCategory());
        s.setPhysicallyhandicapped(true);
        s.setGender(esf.getGender());
        s.setCity(esf.getCity());
        s.setEnrollmentno(esf.getEnrollmentno());
        s.setS_cast(esf.getS_cast());
        s.setS_state(esf.getS_state());
        s.setGender(esf.getGender());
        s.setMname(esf.getMname());
        s.setReligion(esf.getReligion());
        s.setStatus(true);
       
        if (s.update(esf.getId())) {
            System.out.println("Reached here");
            List<Student> li = (List<Student>) request.getSession().getAttribute("student_viewlist");
            for (Student s1 : li) {
                if (s1.getId().equals(s.getId())) {
                    // change all values of s1
                    s1.setId(s.getId());
                    s1.setFname(s.getFname());
                    s1.setLname(s.getLname());
                    s1.setAddress(s.getAddress());
                    s1.setContact(s.getContact());
                    s1.setEmail(s.getEmail());
                    s1.setPassword("");
                    s1.setDateofbirth(s.getDateofbirth());
                    s1.setCategory(s.getCategory());
                    s1.setPhysicallyhandicapped(true);
                    s1.setGender(s.getGender());
                    s1.setCity(s.getCity());
                    s1.setEnrollmentno(s.getEnrollmentno());
                    s1.setS_cast(s.getS_cast());
                    s1.setS_state(s.getS_state());
                    s1.setGender(s.getGender());
                    s1.setMname(s.getMname());
                    s1.setReligion(s.getReligion());
                    s1.setStatus(true);

                }
            }
            request.setAttribute("student_viewlist", li);

            path = "view_student";
        }

        return mapping.findForward(path);
    }
}
